package com.example.sunmiv1.adapter;


import android.content.Context;
import android.content.res.Resources;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.NewReportLossActivity;
import com.example.sunmiv1.entity.LossGoodsInfo;
import com.example.sunmiv1.utils.LogUtil;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReportLossListAdapter extends BaseAdapter {
    // 填充數據的list
    private List<LossGoodsInfo.DataBean> list;
    // 上下文
    private Context context;
    private ACache aCache;
    // 用來導入佈局
    private LayoutInflater inflater;
    private ViewHolder viewHolder = null;
    private static final String TAG = "InventoryListAdapter";

    // 構造器
    public ReportLossListAdapter(Context context, List<LossGoodsInfo.DataBean> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
        aCache = ACache.get(context);
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private int select = 9999;

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
        convertView = inflater.inflate(R.layout.inventory_search_adapter, null);
        // 獲得ViewHolder對象
        viewHolder = new ViewHolder(convertView);
        // 爲view設置標籤
//            convertView.setTag(viewHolder);
//        } else {
//            // 取出holder
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        if (select == position) {
            convertView.setBackgroundResource(R.drawable.red_border);
        } else {
            convertView.setBackgroundResource(R.color.white);
        }
        Resources resources = context.getResources();
        if ("2".equals(list.get(position).getAttribute_state())) {
            viewHolder.inventorySearchAdapterName.setText(resources.getString(R.string.inventory_name) +
                    list.get(position).getGoods_name());
            viewHolder.inventorySearchAdapterSku.setVisibility(View.VISIBLE);
            viewHolder.inventorySearchAdapterSku.setText(resources.getString(R.string.inventory_sku) +
                    list.get(position).getSku_name());
            viewHolder.inventorySearchAdapterFreightNumber.setText(resources.getString(R.string.inventory_freightNumber) +
                    list.get(position).getSku_goods_number());
            viewHolder.inventorySearchAdapterBarcode.setText(resources.getString(R.string.inventory_barcode) + list.get(position).getSku_code());
        } else {
            viewHolder.inventorySearchAdapterName.setText(resources.getString(R.string.inventory_name) + list.get(position).getGoods_name());
            viewHolder.inventorySearchAdapterSku.setVisibility(View.GONE);
            viewHolder.inventorySearchAdapterFreightNumber.setText(resources.getString(R.string.inventory_freightNumber) + list.get(position).getGoods_number());
            viewHolder.inventorySearchAdapterBarcode.setText(resources.getString(R.string.inventory_barcode) + list.get(position).getGoods_code());
        }
        if ("2".equals(aCache.getAsString("userType")) && "1".equals(aCache.getAsString("showPrice"))) {
            viewHolder.inventorySearchAdapterPrice.setVisibility(View.VISIBLE);
            viewHolder.inventorySearchAdapterPrice.setText(resources.getString(R.string.inventory_costPrice) + list.get(position).getCost_price());
        }
        if ("1".equals(aCache.getAsString("userType"))) {
            viewHolder.inventorySearchAdapterPrice.setVisibility(View.VISIBLE);
            viewHolder.inventorySearchAdapterPrice.setText(resources.getString(R.string.inventory_costPrice) + list.get(position).getCost_price());
        }
        if ("1".equals(aCache.getAsString("is_check_show_invenum"))) {
            viewHolder.inventorySearchAdapterStock.setVisibility(View.VISIBLE);
        } else {
            viewHolder.inventorySearchAdapterStock.setVisibility(View.GONE);
        }
        viewHolder.inventorySearchAdapterStock.setText(resources.getString(R.string.inventory_stock) + list.get(position).getGoods_num());
        viewHolder.inventorySearchAdapterInventory.setText(list.get(position).getLoss_num());
        if (NewReportLossActivity.opera) {
            viewHolder.inventorySearchAdapterCheck.setVisibility(View.VISIBLE);
        } else {
            viewHolder.inventorySearchAdapterCheck.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(list.get(position).getCompany())) {
            viewHolder.inventorySearchAdapterUnit.setVisibility(View.GONE);
        } else {
            viewHolder.inventorySearchAdapterUnit.setVisibility(View.VISIBLE);
            viewHolder.inventorySearchAdapterUnit.setText(resources.getString(R.string.purRefundDetail_company) + ":" +
                    list.get(position).getCompany());
        }
        viewHolder.inventorySearchAdapterInventoryName.setText(R.string.reportLossListTip);
        if ("1".equals(list.get(position).getSerial_number_state())) {
            viewHolder.inventorySearchAdapterChooseSerial.setVisibility(View.VISIBLE);
            viewHolder.inventorySearchAdapterInventory.setVisibility(View.GONE);
            Log.e(TAG, "onBindViewHolder: 商品的庫存========" + list.get(position).getLoss_num());
            if (TextUtils.isEmpty(list.get(position).getLoss_num())) {
                viewHolder.inventorySearchAdapterInventoryTv.setText("0");
            } else {
                viewHolder.inventorySearchAdapterInventoryTv.setText(list.get(position).getLoss_num());
            }
        } else {
            viewHolder.inventorySearchAdapterChooseSerial.setVisibility(View.GONE);
            viewHolder.inventorySearchAdapterInventory.setVisibility(View.VISIBLE);
        }
        viewHolder.inventorySearchAdapterCheck.setOnCheckedChangeListener(null);
        viewHolder.inventorySearchAdapterCheck.setChecked(list.get(position).isChecked());
        viewHolder.inventorySearchAdapterCheck.setOnCheckedChangeListener((buttonView, isChecked) -> {
            list.get(position).setChecked(isChecked);
            notifyDataSetChanged();
        });
        if ("2".equals(list.get(position).getAttribute_state())) {
            viewHolder.inventorySearchAdapterInventory.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
        } else {
            if ("1".equals(list.get(position).getPricing_method())) {
                viewHolder.inventorySearchAdapterInventory.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
            } else {
                viewHolder.inventorySearchAdapterInventory.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
            }
        }
        final EditText inventory = viewHolder.inventorySearchAdapterInventory;
        inventory.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                LogUtil.i(TAG + "onTextChanged: charSequence=" + charSequence.toString());
                if (charSequence.toString().contains(".")) {
                    if (-1 != charSequence.toString().indexOf(".", charSequence.toString().indexOf(".") + 1)) {
                        inventory.setText(charSequence.toString().
                                substring(0, charSequence.toString().length() - 1));
                        inventory.setSelection(charSequence.length() - 1);
                    } else {
                        if (charSequence.length() - 1 - charSequence.toString().indexOf(".") > 3) {
                            charSequence = charSequence.toString().subSequence(0, charSequence.toString().indexOf(".") + 4);
                            inventory.setText(charSequence);
                            inventory.setSelection(charSequence.length());
                        }
                    }
                }
                if (charSequence.toString().trim().substring(0).equals(".")) {
                    charSequence = "0" + charSequence;
                    inventory.setText(charSequence);
                    inventory.setSelection(2);
                }

                if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                    if (!charSequence.toString().substring(1, 2).equals(".")) {
                        inventory.setText(charSequence.subSequence(1, 2));
                        inventory.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setLoss_num(inventory.getText().toString().trim());
            }
        });
        viewHolder.inventorySearchAdapterRemarkLin.setVisibility(View.VISIBLE);
        viewHolder.inventorySearchAdapterRemark.setText(list.get(position).getRemark());
        final EditText remark = viewHolder.inventorySearchAdapterRemark;
        remark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                list.get(position).setRemark(remark.getText().toString().trim());
            }

            @Override
            public void afterTextChanged(Editable s) {
//                list.get(position).setRemark(remark.getText().toString().trim());
            }
        });
        viewHolder.llPurchaseOrderChoose.setVisibility(View.GONE);
        viewHolder.inventorySearchAdapterChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choose.choose(position);
            }
        });
        return convertView;
    }
    private Choose choose;
    public void setChoose(Choose choose) {
        this.choose = choose;
    }

    public interface Choose {
        void choose(int index);
    }

    static class ViewHolder {
        @BindView(R.id.inventorySearch_adapter_check)
        CheckBox inventorySearchAdapterCheck;
        @BindView(R.id.inventorySearch_adapter_name)
        TextView inventorySearchAdapterName;
        @BindView(R.id.inventorySearch_adapter_sku)
        TextView inventorySearchAdapterSku;
        @BindView(R.id.inventorySearch_adapter_freightNumber)
        TextView inventorySearchAdapterFreightNumber;
        @BindView(R.id.inventorySearch_adapter_price)
        TextView inventorySearchAdapterPrice;
        @BindView(R.id.inventorySearch_adapter_barcode)
        TextView inventorySearchAdapterBarcode;
        @BindView(R.id.inventorySearch_adapter_stock)
        TextView inventorySearchAdapterStock;
        @BindView(R.id.inventorySearch_adapter_inventory)
        EditText inventorySearchAdapterInventory;
        @BindView(R.id.inventorySearch_adapter_inventoryName)
        TextView inventorySearchAdapterInventoryName;
        @BindView(R.id.inventorySearch_adapter_remarkLin)
        LinearLayout inventorySearchAdapterRemarkLin;
        @BindView(R.id.inventorySearch_adapter_remark)
        EditText inventorySearchAdapterRemark;
        @BindView(R.id.ll_purchase_order_choose)
        LinearLayout llPurchaseOrderChoose;
        @BindView(R.id.inventorySearch_adapter_unit)
        TextView inventorySearchAdapterUnit;
        @BindView(R.id.inventorySearch_adapter_chooseSerial)
        LinearLayout inventorySearchAdapterChooseSerial;
        @BindView(R.id.inventorySearch_adapter_choose)
        TextView inventorySearchAdapterChoose;
        @BindView(R.id.inventorySearch_adapter_inventoryTv)
        TextView inventorySearchAdapterInventoryTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
